Trò chơi game bắn súng đơn giản trong UNITY Engine
2 using System.Collections;
3
4 public class AspectCamera : MonoBehaviour {
5
6 void Start()
7 {
8 // set the desired aspect ratio (the values in this example are
9 // hard-coded for 16:9, but you could make them into public
10 // variables instead so you can set them at design time)
11 float targetaspect = 16.0f / 9.0f;
12
13 // determine the game window's current aspect ratio
14 float windowaspect = (float)Screen.width / (float)Screen.height;
15
16 // current viewport height should be scaled by this amount
17 float scaleheight = windowaspect / targetaspect;
18
19 // obtain camera component so we can modify its viewport
20 Camera camera = GetComponent<Camera>();
21
22 // if scaled height is less than current height, add letterbox
23 if (scaleheight < 1.0f)
24 {
25 Rect rect = camera.rect;
26
27 rect.width = 1.0f;
28 rect.height = scaleheight;
29 rect.x = 0;
30 rect.y = (1.0f - scaleheight) / 2.0f;
31
32 camera.rect = rect;
33 }
34 else // add pillarbox
35 {
36 float scalewidth = 1.0f / scaleheight;
37
38 Rect rect = camera.rect;
39
40 rect.width = scalewidth;
41 rect.height = 1.0f;
42 rect.x = (1.0f - scalewidth) / 2.0f;
43 rect.y = 0;
44
45 camera.rect = rect;
46 }
47 }
48
49 // Update is called once per frame
50 void Update () {
51
52 }
53 }
set the desired aspect ratio (the values in this example are
hard-coded for 16:9, but you could make them into public
variables instead so you can set them at design time)
determine the game window's current aspect ratio
current viewport height should be scaled by this amount
obtain camera component so we can modify its viewport
if scaled height is less than current height, add letterbox
else add pillarbox
Update is called once per frame